﻿using System;
using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Character;
using Gp.Scripts.Database;
using Gp.Scripts.Core;
using Helpers;
using QFramework;
using Sirenix.OdinInspector;
# if UNITY_EDITOR
using Sirenix.Utilities.Editor;
# endif

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;

namespace Gp.Scripts.Core {
    public enum Method {
        FindOrSpawn,
        SpawnNew,
    }



    [ExecuteAlways]
    public class  PlaceHolder : MonoBehaviour {
        [LabelText("是否是玩家角色")] [SerializeField] [OnValueChanged("OnPlayerFlagChanged")]
        private bool isPlayer = false;

        [LabelText("生成策略")] [HideIf("isPlayer")]
        public Method method = Method.FindOrSpawn;

        [HideLabel] [HideIf("isPlayer")] [SerializeField]
        private EntityConfigInfo info;

        public EntityConfigInfo Info => info;

        private BaseCharacter _cache;


        private BaseCharacter FindOrSpawn() {
            var res = Global.Get<EntitySystem>().FindCharacter(info.name);
            return res != null ? res : SpawnNew();
        }


        private BaseCharacter SpawnNew() {
            throw new NotImplementedException();
            // var res = Global.Get<EntitySystem>().BuildCharacter(info.name);
            // res.transform.position = this.transform.position;
            // return res;
        }

        public BaseCharacter GetCharacter() {
            if (isPlayer) {
                var player = ObjectHelper.PlayerCharacter;
                return player;
            }


            // 如果已经进行过查找或生成，且目标尚未被销毁，则不会再次进行查找
            if (!_cache) {
                _cache = method switch {
                    Method.FindOrSpawn => FindOrSpawn(),
                    Method.SpawnNew => SpawnNew(),
                    _ => throw new ArgumentOutOfRangeException()
                };
            }

            return _cache;
        }


#if UNITY_EDITOR

        private void OnEnable() {
            SceneView.duringSceneGui += OnSceneGUI;
        }

        private void OnDisable() {
            SceneView.duringSceneGui -= OnSceneGUI;
        }

        private const int MaxDistance = 25;

        private void OnSceneGUI(SceneView obj) {
            var distance = Vector3.Distance(SceneView.lastActiveSceneView.camera.transform.position,
                transform.position);

            if (distance > MaxDistance || !gameObject.activeInHierarchy) {
                return;
            }


            string value = string.IsNullOrEmpty(info.name) ? "None" : info.name;

            Handles.Label(transform.position,
                info.portrait != null
                    ? new GUIContent(value, info.portrait.ResizeByBlit(30, 30))
                    : new GUIContent(value));
        }

        private void OnDrawGizmos() {
            if (isPlayer) {
                Gizmos.color = Color.yellow;
            }
            else {
                Gizmos.color = Color.cyan;
            }

            Gizmos.DrawCube(transform.position, Vector3.one / 3);
        }

        [HideInInspector] [SerializeField] private EntityConfigInfo cache;

        private void OnPlayerFlagChanged() {
            if (isPlayer) {
                cache = info;
                info = new EntityConfigInfo(PlayerConst. MainCharacterUnitConfigId );
            }
            else {
                info = cache;
            }
        }


        [Button("重命名物体")]
        private void SetGameObjectName() {
            if (info != null && !info.name.IsNullOrEmpty())
                gameObject.name = info.name;
        }
#endif
    }
}